package com.demo.service.dataManagement;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

/**
 * @Classname PoemService
 * @Description TODO
 * @Date 2020/3/13 17:16
 * @Created by yuanhang
 */
public class PoemService {
    /**
     * @return
     * @throws
     * @description 条件查询
     * @author yuanhang
     * @date 2020/3/13 17:18
     * @Param
     */
    public Page<Record> condition(String poemTitle, int page, int limit) {
        Kv cond = Kv.by("poem_title", poemTitle);
        Page<Record> recordList = Db.template("poem.condition", cond).paginate(page, limit);
        //增加序号、整理创作日期
        for (int i = 0; i < recordList.getList().size(); i++) {
            Record record = recordList.getList().get(i);
            //序号
            record.set("index", i + 1);
            //创作日期
            String poemDate = "";
            Integer poemYear = record.getInt("poem_year");
            if (poemYear != null) {
                if (poemYear > 0) {
                    //公元默认不显示
                    poemDate = poemYear + "年";
                } else {
                    int year = 0 - poemYear.intValue();//取正数
                    poemDate = "公元前" + year + "年";
                }
            }
            Integer poemMonth = record.getInt("poem_month");
            if (poemMonth != null && (poemMonth > 0)) {
                poemDate = poemDate + poemMonth + "月";
            }
            Integer poemDay = record.getInt("poem_day");
            if (poemDay != null && (poemDay > 0)) {
                poemDate = poemDate + poemDay + "日";
            }
            record.set("poem_date", poemDate);

        }
        return recordList;
    }


    /**
     * @return
     * @throws
     * @description 查询诗人
     * @author yuanhang
     * @date 2020/3/16 19:11
     * @Param
     */
    public List<Record> poet(String name) {
        Kv cond = Kv.by("name", name);
        List<Record> recordList = Db.template("poem.poet", cond).find();
        return recordList;
    }
}
